package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.CuccServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalPortBandwidthAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月16日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(LocalPortBandwidthAttrHandler.class)
public class LocalPortBandwidthAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;
    /**
     * 接入电路带宽 默认值等于'端口带宽'
     * 计费带宽   默认值等于'端口带宽'
     */

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (Objects.isNull(attr)) {
            return;
        }
        // 2_0000077	计费带宽
        // 2_0000070	接入电路带宽
        // 计费带宽
        KiteAttr billingBandwidthAttr = inst.getAttr("2_0000077");
        //接入电路带宽
        KiteAttr accessCircuitAttr = inst.getAttr("2_0000070");
        if(KiteStringUtils.isNotEmpty(attr.getValue())){
            if(!Objects.isNull(billingBandwidthAttr)&& KiteListUtils.isNotEmpty(billingBandwidthAttr.getAttrValues()) && billingBandwidthAttr.getAttrValues().toString().contains(attr.getValue())){
                billingBandwidthAttr.setDefaultValue(attr.getValue());
                billingBandwidthAttr.setValue(attr.getValue());
                billingBandwidthAttr.setValueDesc(attr.getValueDesc());
            }
            if(!Objects.isNull(accessCircuitAttr) && KiteListUtils.isNotEmpty(accessCircuitAttr.getAttrValues()) && accessCircuitAttr.getAttrValues().toString().contains(attr.getValue())){
                accessCircuitAttr.setDefaultValue(attr.getValue());
                accessCircuitAttr.setValue(attr.getValue());
                accessCircuitAttr.setValueDesc(attr.getValueDesc());
            }
        }

        //安徽个性需求
        //获取省份标识
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        //业务等级
        KiteAttr ywdjKiteAttr = inst.getAttr(LocalKeyConsts.YWDJ_FIELD_NAME);
        if (LocalKeyConsts.PROVINCE_AHHUI.equals(provinceCode)) {
            IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
            String nodeCode = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "nodeCode");
            String serviceOfferId = data.getServiceOfferId();
            // app甩单补录、资源核查补录情况
            String renderOrderType = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "renderOrderType");
            if(KiteStringUtils.equalsAny(nodeCode,"OrderSigned","OrderResubmit","OrderAudit","OrderInput")||
                KiteStringUtils.equalsAny(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW,"1","2826",LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK,LocalServiceOfferConsts.SERVICE_OFFER_TRANSFER_TO_ORDER, CuccServiceOfferConsts.SERVICE_OFFER_BUSINESS_CHANGE)
                || KiteStringUtils.isEqual(renderOrderType, "orderRejection")){
                List<KiteAttrValue> yedjAttrList = ywdjKiteAttr.getAttrValues();
                //业务等级“优享专线宽带”去掉后就没有了，需要先判断，再补全
                boolean needAddFlag = true;
                for (KiteAttrValue value:yedjAttrList) {
                    if(KiteStringUtils.equals("110009540",value.getAttrValue())){
                        //如果有的话，则不需要补
                        needAddFlag = false;
                    }
                }
                if(needAddFlag){
                    KiteAttrValue yxzxkdAttrValue = new KiteAttrValue();
                    yxzxkdAttrValue.setAttrValue("110009540");
                    yxzxkdAttrValue.setAttrId("20210313680");
                    yxzxkdAttrValue.setAttrValueName("优享专线宽带");
                    ywdjKiteAttr.getAttrValues().add(yxzxkdAttrValue);
                }
                //在端口带宽为“100M”“200M”的时候展示
                if(!KiteStringUtils.equalsAny(attr.getValue(),"1_0000289","1_0000295")){
                    //业务等级去掉“优享专线宽带”
                    ywdjKiteAttr.getAttrValues().removeIf(yedjAttr -> KiteStringUtils.equals(yedjAttr.getAttrValue(), "110009540"));
                    ywdjKiteAttr.setValue("");
                    ywdjKiteAttr.setValueDesc("");
                    operateInstAttrService.setAttrValue(inst, LocalKeyConsts.YWDJ_FIELD_NAME, "", data.getSceneInstId(), null);
                    //业务子类型
                    KiteAttr ywzlxKiteAttr = inst.getAttr(LocalKeyConsts.YWZLX_FIELD_NAME);
                    ywzlxKiteAttr.getAttrValues().removeIf(ywzlxAttr -> KiteStringUtils.equals(ywzlxAttr.getAttrValue(), "110009541"));
                    ywzlxKiteAttr.setValue("100003212");
                    ywzlxKiteAttr.setValueDesc("普通互联网专线");
                    operateInstAttrService.setAttrValue(inst, LocalKeyConsts.YWZLX_FIELD_NAME, "100003212", data.getSceneInstId(), null);
                    KiteAttr sxslKiteAttr = inst.getAttr(LocalKeyConsts.SXSL_FIELD_NAME);
                    KiteAttr xxslKiteAttr = inst.getAttr(LocalKeyConsts.XXSL_FIELD_NAME);
                    if (Objects.nonNull(sxslKiteAttr)) {
                        sxslKiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                    }
                    if (Objects.nonNull(xxslKiteAttr)) {
                        operateInstAttrService.setAttrValue(inst, LocalKeyConsts.XXSL_FIELD_NAME, "", data.getSceneInstId(), null);
                        xxslKiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                    }
                }
            }
        }else{
            //业务等级非安微省份没有“优享专线宽带”
            KiteAttrValue kiteAttrValueRemove = null;
            if(ywdjKiteAttr.getAttrValues() != null && ywdjKiteAttr.getAttrValues().size()>0) {
                for (KiteAttrValue kiteAttrValue : ywdjKiteAttr.getAttrValues()) {
                    if("110009540".equals(kiteAttrValue.getAttrValue())){
                        kiteAttrValueRemove = kiteAttrValue;
                    }
                }
            }
            if(null != kiteAttrValueRemove){
                ywdjKiteAttr.getAttrValues().remove(kiteAttrValueRemove);
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

}
